package com.pp.admin.common.server.entities;

import java.io.Serializable;
import java.util.Date;

import javax.jdo.PersistenceManager;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;
import com.pp.admin.common.server.PersistenceManagerFactory;

/**
 * The persistent class for the PP_EMAILS database table.
 */
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Email extends Base implements Serializable {

	private static final long serialVersionUID = 1L;

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key id;

	@Persistent
	private String emailId;

	@Persistent
	private boolean primary;

	@Persistent
	private Key createdBy;

	@Persistent
	private Date createdByTime;

	@Persistent
	private Key lastModefiedBy;

	@Persistent
	private Date lastModefiedByTime;

	// bi-directional many-to-one association to Contact
	@Persistent
	private Contact contact;

	public Email() {

	}

	/**
	 * @return the id
	 */
	public Key getId() {

		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(Key id) {

		this.id = id;
	}

	/**
	 * @return the emailId
	 */
	public String getEmailId() {

		return emailId;
	}

	/**
	 * @param emailId
	 *            the emailId to set
	 */
	public void setEmailId(String emailId) {

		this.emailId = emailId;
	}

	/**
	 * @return the primary
	 */
	public boolean isPrimary() {

		return primary;
	}

	/**
	 * @param primary
	 *            the primary to set
	 */
	public void setPrimary(boolean primary) {

		this.primary = primary;
	}

	/**
	 * @return the contact
	 */
	public Contact getContact() {

		return contact;
	}

	/**
	 * @param contact
	 *            the contact to set
	 */
	public void setContact(Contact contact) {

		this.contact = contact;
	}

	/**
	 * @return the createdBy
	 */
	@Override
	public User getCreatedBy() {

		User user = null;
		PersistenceManager pm = PersistenceManagerFactory
				.getPersistenceManager();
		try {
			user = pm.getObjectById(User.class, this.createdBy);
		} finally {
			PersistenceManagerFactory.closePersistenceManager();
		}
		return user;
	}

	/**
	 * @param createdBy
	 *            the createdBy to set
	 */
	@Override
	public void setCreatedBy(Key createdBy) {

		this.createdBy = createdBy;
	}

	/**
	 * @return the createdByTime
	 */
	@Override
	public Date getCreatedByTime() {

		return createdByTime;
	}

	/**
	 * @param createdByTime
	 *            the createdByTime to set
	 */
	@Override
	public void setCreatedByTime(Date createdByTime) {

		this.createdByTime = createdByTime;
	}

	/**
	 * @return the lastModefiedBy
	 */
	@Override
	public User getLastModefiedBy() {

		User user = null;
		PersistenceManager pm = PersistenceManagerFactory
				.getPersistenceManager();
		try {
			user = pm.getObjectById(User.class, this.lastModefiedBy);
		} finally {
			PersistenceManagerFactory.closePersistenceManager();
		}
		return user;
	}

	/**
	 * @param lastModefiedBy
	 *            the lastModefiedBy to set
	 */
	@Override
	public void setLastModefiedBy(Key lastModefiedBy) {

		this.lastModefiedBy = lastModefiedBy;
	}

	/**
	 * @return the lastModefiedByTime
	 */
	@Override
	public Date getLastModefiedByTime() {

		return lastModefiedByTime;
	}

	/**
	 * @param lastModefiedByTime
	 *            the lastModefiedByTime to set
	 */
	@Override
	public void setLastModefiedByTime(Date lastModefiedByTime) {

		this.lastModefiedByTime = lastModefiedByTime;
	}

	public Email importEmail(com.pp.admin.common.client.beans.Email email) {

		/**
		 * TODO: add server side validation for email type.
		 */
		this.emailId = email.getEmailId();
		this.primary = email.isPrimary();

		return this;
	}

	public com.pp.admin.common.client.beans.Email exportEmail() {
		com.pp.admin.common.client.beans.Email email = new com.pp.admin.common.client.beans.Email();
		email.setId(this.id.getId());
		email.setEmailId(this.emailId);
		email.setPrimary(this.primary);
		return email;
	}

}
